﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Agile.Engine.Data;
using Agile.Engine.Utils;

namespace Fis.Web.Rules.Book
{
    public class AidsRule : DynamicRule
    {

        string TableName = "TAIDS";

        void AddNew(System.Data.DataTable data)
        {
            var valid = Model.QueryTable(TableName, string.Format("(FCODE='{0}' OR FNAME='{1}') AND FGROUP='{2}'",
                data.Rows[0]["FCODE"], data.Rows[0]["FNAME"], data.Rows[0]["FGROUP"]));

            if (valid.Rows.Count > 0)
            {
                Status = -1;
                Message = "编码或名称已存在";
                return;
            }

            if (data.Rows[0]["FDEFAULT"].ToString() == "1")
            {
                Model.ExecuteNonQuery(string.Format(
                    "UPDATE TAIDS SET FDEFAULT=0,SINDEXDATE=GETDATE() WHERE FDEFAULT=1 AND FGROUP='{0}'"
                    , data.Rows[0]["FGROUP"]));
            }

            data.Rows[0]["SID"] = Guid.NewGuid().ToString();
            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            data.Rows[0]["SBOOKID"] = BookID;
            data.AcceptChanges();
            data.Rows[0].SetAdded();
            data.TableName = TableName;
            Model.UpdateTable(data);
        }

        void Modify(System.Data.DataTable data)
        {
            var valid = Model.QueryTable(TableName,
                string.Format("(FCODE='{0}' OR FNAME='{1}') AND SID!='{2}'  AND FGROUP='{3}'"
                    , data.Rows[0]["FCODE"]
                    , data.Rows[0]["FNAME"]
                    , data.Rows[0]["SID"]
                    , data.Rows[0]["FGROUP"]));
            if (valid.Rows.Count > 0)
            {
                Status = -1;
                Message = "编码或名称已存在";
                return;
            }

            if (data.Rows[0]["FDEFAULT"].ToString() == "1")
            {
                Model.ExecuteNonQuery(string.Format(
                    "UPDATE TAIDS SET FDEFAULT=0,SINDEXDATE=GETDATE() WHERE FDEFAULT=1 AND FGROUP='{0}'"
                    , data.Rows[0]["FGROUP"]));
            }

            data.Rows[0]["SINDEXDATE"] = DateTime.Now;
            data.AcceptChanges();
            data.Rows[0].SetModified();
            data.TableName = TableName;
            Model.UpdateTable(data);
        }

        void Delete(string sid)
        {
            var count = Model.ExecuteScalar(0, "SELECT COUNT(*) FROM TBILLDETAIL WHERE FHSVALUE like '%" + sid + "%'");
            if (count > 0)
            {
                Status = -1;
                Message = "核算项目已被使用，不能删除";
            }

            Model.Delete(TableName, "SID='" + sid + "'");
            CommonRule common = new CommonRule();
            common.UpdateDeleteInfo(Model.DataContext, TableName, sid, BookID);

        }

        public override void ExecuteNonTrans()
        {
            var action = Paramater["Action"].ToString();
            System.Data.DataTable data = null;

            switch (action)
            {
                case "AddNew":
                    data = ZipUtils.UnZipDataTable(Paramater["viewData"].ToString());
                    AddNew(data);
                    break;
                case "Modify":
                    data = ZipUtils.UnZipDataTable(Paramater["viewData"].ToString());
                    Modify(data);
                    break;
                case "Delete":
                    Delete(Paramater["SID"].ToString());
                    break;
            }

            if (data != null)
                Result["viewData"] = ZipUtils.ZipDataTable(data);

            Status = 1;
        }
    }
}
